package com.design.YiTong.msm.controller;

import com.design.YiTong.common.result.Result;
import com.design.YiTong.msm.service.MsmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;

    @RestController
    @RequestMapping("/api/msm")
    public class MsmApiController {
        @Autowired
        private MsmService msmService;
        @Autowired
        private RedisTemplate<String,String> redisTemplate;
        @GetMapping("send/{phone}")
        public Result sendCode(@PathVariable String phone){
            String chackCode=String.valueOf(new Random().nextInt(899999)+100000);
            try{
                msmService.send(chackCode,phone);
                redisTemplate.opsForValue().set(phone,chackCode,2, TimeUnit.MINUTES);
            }catch (Exception e){
                return Result.fail();
            }
            return Result.ok(chackCode);
        }
    }
